#ifndef _OCTREENODE_H_
#define _OCTREENODE_H_

#include <memory>
#include <SEngine/Render/Entity/BoundingBox.h>
#include <SECore/Descriptor.h>

namespace SEngine
{
    class Octree;
    class OctreeNode : public std::enable_shared_from_this<OctreeNode>, public AnyDescriptor
    {
    public:
        OctreeNode(Octree& tree, BoundingBox boundingBox);
        virtual ~OctreeNode() = default;

        template<typename F>
        void ForeachChildren(F&& fn) {
            for (auto& weak_child : m_children) {
                if (auto child = weak_child.lock()) {
                    fn(*child);
                }
            }
        }

        BoundingBox & GetBoundingBox() { return m_boundingBox; }

        void Split();

        bool IsLeaf() { return m_children.empty(); }

    private:
        Octree & m_tree;
        BoundingBox m_boundingBox;
        std::vector<std::weak_ptr<OctreeNode>> m_children;
    };
} // namespace SEngine


#endif